package com.lmt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 获取服务提供者信息
 * Date 2018/07/21
 *
 * @author MT-Lin
 */
@RestController
public class ProviderController {

    private final LoadBalancerClient loadBalancerClient;

    private final DiscoveryClient discoveryClient;

    @Autowired
    public ProviderController(LoadBalancerClient loadBalancerClient, DiscoveryClient discoveryClient) {
        this.loadBalancerClient = loadBalancerClient;
        this.discoveryClient = discoveryClient;
    }

    /**
     * 获取所有ServiceId为provider-consul的所有服务
     * @return 所有服务
     */
    @RequestMapping("/getAllProvider")
    public Object getAllProvider(){
        return discoveryClient.getInstances("provider-consul");
    }

    /**
     * 从所有ServiceId为provider-consul的服务提供者中选择一个服务（默认：轮询）
     * @return 一个服务
     */
    @RequestMapping("/getOneProvider")
    public Object getOneProvider(){
        return loadBalancerClient.choose("provider-consul").getUri().toString();
    }
}
